<?php
// 启动会话
session_start();

// 获取从前端发送过来的数据
$customerName = $_POST['customerName'] ?? '';
$customerPhone = $_POST['customerPhone'] ?? '';
$roomType = $_POST['roomType'] ?? '';
$roomCount = $_POST['roomCount'] ?? 0;
$checkinDate = $_POST['checkinDate'] ?? '';
$checkoutDate = $_POST['checkoutDate'] ?? '';
$adultCount = $_POST['adultCount'] ?? 0;
$childCount = $_POST['childCount'] ?? 0;
$scheduledTime = $_POST['scheduledTime'] ?? '';
$roomNumber = $_POST['roomNumber'] ?? '';
$paymentMethod = $_POST['paymentMethod'] ?? '';
$bookingMethod = $_POST['bookingMethod'] ?? '';
$remarks = $_POST['remarks'] ?? '';

$account_phone = $_SESSION['phone'];

// 连接数据库
require_once 'databaseConnection.php';
try {
    $conn = connectToDatabase();
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // 插入数据
    $stmt = $conn->prepare("INSERT INTO predetermine (customerName, customerPhone, roomType, roomCount, checkinDate, checkoutDate, adultCount, childCount, scheduledTime, roomNumber, paymentMethod, bookingMethod,account_phone, remarks)
    VALUES (:customerName, :customerPhone, :roomType, :roomCount, :checkinDate, :checkoutDate, :adultCount, :childCount, :scheduledTime, :roomNumber, :paymentMethod, :bookingMethod, :account_phone, :remarks)");
    $stmt->bindParam(':customerName', $customerName);
    $stmt->bindParam(':customerPhone', $customerPhone);
    $stmt->bindParam(':roomType', $roomType);
    $stmt->bindParam(':roomCount', $roomCount, PDO::PARAM_INT);
    $stmt->bindParam(':checkinDate', $checkinDate);
    $stmt->bindParam(':checkoutDate', $checkoutDate);
    $stmt->bindParam(':adultCount', $adultCount, PDO::PARAM_INT);
    $stmt->bindParam(':childCount', $childCount, PDO::PARAM_INT);
    $stmt->bindParam(':scheduledTime', $scheduledTime);
    $stmt->bindParam(':roomNumber', $roomNumber);
    $stmt->bindParam(':paymentMethod', $paymentMethod);
    $stmt->bindParam(':bookingMethod', $bookingMethod);
    $stmt->bindParam(':account_phone', $account_phone);
    $stmt->bindParam(':remarks', $remarks);

    $stmt->execute();
    $response = array('success' => true, 'message' => '预订成功');
    echo json_encode($response);

} catch (PDOException $e) {
    $response = array('success' => false, 'message' => '预订失败'. $e->getMessage());
    echo json_encode($response);
}

$conn = null;
?>